<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>获取数据</button>
    <div></div>
    <script>
        // 1. 定义ajax函数 ajax(cb)
        function ajax() {
            return new Promise((res, rej) => {
                let x = new XMLHttpRequest();
                x.open('get', 'https://v1.hitokoto.cn/?c=a');
                x.send();
                x.responseType = 'json';
                x.onreadystatechange = function () {
                    if (x.readyState === 4) {
                        if (x.status >= 200 && x.status < 300) {
                           res(x.response)
                        }else{
                            rej(x.status)
                        }
                    }
                }
            })
        }
        // 2. 获取按钮
        let btn = document.querySelector('button')
        // 3. 添加点击事件
        btn.onclick = function () {
            ajax()
            .then(val=>{
                console.log(val);
            },rea=>{
                console.log(rea);
            })
        }
            // 3.1 调用ajax方


    </script>
</body>

</html>